package pers.zh.parking.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import pers.zh.parking.common.BaseController;
import pers.zh.parking.common.Const;
import pers.zh.parking.common.Response;
import pers.zh.parking.po.LotQueryBean;
import pers.zh.parking.po.ParkingLot;
import pers.zh.parking.service.ILotService;
import pers.zh.parking.utils.GsonUtils;
import pers.zh.parking.utils.ReadJsonUtil;

import java.lang.reflect.Type;
import java.util.List;

@RestController
@RequestMapping("lot")
public class LotController extends BaseController {

    @Autowired
    ILotService lotService;

    /**
     * 自动读取json文件生成数据 from:static/lot.json
     * @return
     */
    @PostMapping("/generate")
    public Response generate() {
        try {
            String json = ReadJsonUtil.readFromFile("lot.json");
            if (StringUtils.isEmpty(json)) {
                return Response.fail_500("json文件读取失败");
            }
            Type type = new TypeToken<List<ParkingLot>>(){}.getType();
            List<ParkingLot> lots = GsonUtils.fromJson(json, type);
            if(ObjectUtils.isEmpty(lots)) {
                return Response.fail_500("json解析失败");
            }
            if (lotService.batchInsert(lots)) {
                return Response.ok(lots, "停车场数据写入成功");
            }
            return Response.fail_500("停车场数据写入失败，请查看log");
        } catch (Exception e) {
            e.printStackTrace();
            return Response.fail_500(e.getMessage());
        }
    }

    /**
     * 根据当前位置经纬度查询指定范围内停车场信息
     * @param queryBean
     * @return
     */
    @PostMapping("list/around")
    public Response around(@RequestBody LotQueryBean queryBean ) {
        if (queryBean == null) {
            return Response.fail_400("位置信息错误");
        }
        if (queryBean.getLatitude() == 0 && queryBean.getLongitude() == 0) {
            return Response.fail_400("位置信息错误");
        }
        if (queryBean.getRadius() == null || queryBean.getRadius() == 0) {
            // 范围默认为1000米
            queryBean.setRadius(1000);
        }
        List<ParkingLot> list = lotService.getLotsAround(queryBean);
        return Response.ok(list);
    }

    /**
     * 搜索停车场
     * @param queryBean
     * @return
     */
    @PostMapping("list/search")
    public Response search(@RequestBody LotQueryBean queryBean) {
        if (queryBean == null) {
            return list();
        }
        if (queryBean.getLatitude() == 0 && queryBean.getLongitude() == 0) {
            return Response.fail_400("位置信息错误");
        }
        // 设置默认搜索范围
//        queryBean.setRadius(50000);
        List<ParkingLot> list = lotService.getLotsAround(queryBean);
        return Response.ok(list);
    }

    private Response list() {
        List<ParkingLot> list = lotService.getAllLots();
        return Response.ok(list);
    }

    @PostMapping(value = "list")
    public Response pageList(@RequestBody PageInfo info) {
        if (info == null) {
            return Response.fail_500("参数为空");
        }
        if (info.getPageNum() == 0) {
            info.setPageNum(Const.PageParams.DEFAULT_PAGE_NUM);
        }
        if (info.getPageSize() == 0) {
            info.setPageSize(Const.PageParams.DEFAULT_PAGE_SIZE);
        }
        PageHelper.startPage(info.getPageNum(), info.getPageSize());
        List<ParkingLot> list = lotService.getAllLots();
        return Response.ok(new PageInfo<>(list), "查询成功");
    }


}
